package com.yerbamateprimer.model;

import java.io.Serializable;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.google.common.base.Preconditions;

/**
 * This class models a user
 * of the yerba mate primer.
 * 
 * @author Matias Liwski
 * @version 1.0
 */
public class User implements Serializable{
	private static final long serialVersionUID = 6525657340589932532L;
	private Long id;
	private String name;

	/**
	 * @param id The id of the user.
	 * @param name The default name of the user, if none has defined at the client.
	 */
	public User(Long id, String name) {
		setId(id);
		setName(name);
	}

	/**
	 * @return The default name of the user, if none has defined at the client.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return The id of the user.
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id The id of the user, to set.
	 */
	public void setId(Long id) {
		Preconditions.checkArgument(id != null && id > 0, "The id has to be greather than zero (id=" + id + ").");
		this.id = id;
	}

	/**
	 * @param name The default name of the user, if none has defined at the client, to set.
	 */
	public void setName(String name) {
		Preconditions.checkArgument(name != null && !name.equals(""), "The name couldn't be null or empty (name=" + name + ").");
		this.name = name;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof User))
			return false;
		User other = (User) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE, false, false);
	}
}